package com.itguigu.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itguigu.auth.service.SysUserService;
import com.itguigu.common.result.Result;
import com.itguigu.common.utils.MD5;
import com.itguigu.model.system.SysUser;
import com.itguigu.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *
 */
@Slf4j
@RestController
@Api(tags = "用户管理")
@RequestMapping("/admin/system/sysUser")
@CrossOrigin  // 解决跨域地问题
public class SysUserController {

    @Autowired
    private SysUserService service;

    //用户条件分页查询
    @ApiOperation(value = "用户条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page ,
                        @PathVariable Long limit,
                        SysUserQueryVo sysUserQueryVo) {
        log.info("根据条件分页查询 , page : {} , limit : {} , sysUserQueryVo : {}" , page , limit , sysUserQueryVo);
        // 创建page对象
        Page<SysUser> pageParam = new Page<>(page, limit);
        // 封装判断条件
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        // 获取条件值
        String username = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        // 判单条件值
        if (StringUtils.hasLength(username)){
            queryWrapper.like(SysUser::getUsername, username);
        }
        // 时间范围
        if (StringUtils.hasLength(createTimeBegin)){
            queryWrapper.ge(SysUser::getCreateTime, createTimeBegin);
        }
        if (StringUtils.hasLength(createTimeEnd)){
            queryWrapper.le(SysUser::getCreateTime, createTimeEnd);
        }
        // 分页查询
        IPage<SysUser> pageModel = service.page(pageParam, queryWrapper);
        return Result.success(pageModel);
    }

    // 保存用户
    @PostMapping("save")
    @ApiOperation(value = "保存用户")
    public Result save(@RequestBody SysUser user) {
        log.info("获取的用户数据user : {}", user);
        // md5 加密操作
        // 获取密码 并 加密
        String passwordMD5 = MD5.encrypt(user.getPassword());
        user.setPassword(passwordMD5);

        boolean save = service.save(user);
        return Result.success();
    }

    // 更新用户
    @ApiOperation(value = "更新用户")
    @PutMapping("update")
    public Result updateById(@RequestBody SysUser user) {
        log.info("要修改的用户id为 : {} , 数据为 user : {} ", user.getId(), user);
        boolean update = service.updateById(user);
        return Result.success();
    }

    // 删除用户
    @DeleteMapping("remove/{id}")
    @ApiOperation(value = "通过id删除用户")
    public Result remove(@PathVariable Long id) {
        log.info("删除的用户id为 : {}", id);
        boolean b = service.removeById(id);
        return Result.success();
    }

    // 获取用户
    @ApiOperation(value = "通过id获取用户信息")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        log.info("获取指定id的用户信息 : {}", id);
        SysUser user = service.getById(id);
        return Result.success(user);
    }

    @ApiOperation(value = "更新状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id , @PathVariable Integer status){
        log.info("根据id修改状态 , id : {} , status : {}" , id , status);
        service.updateStatus(id , status);
        return Result.success();
    }

    @ApiOperation(value = "我的--获取当前用户地基本信息")
    @GetMapping("getCurrentUser")
    public Result getCurrentUser() {
        log.info("我的--获取当前用户地基本信息");
        return Result.success(service.getCurrentUser());
    }
}
